package com.ruoyi.web.controller.market.extend;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.db.domain.CountInfo;
import com.ruoyi.db.model.param.ChannelInfoParam;
import com.ruoyi.db.model.param.CountInfoParam;
import com.ruoyi.extend.enums.ChannelTypeEnum;
import com.ruoyi.extend.service.IChannelInfoService;
import com.ruoyi.extend.service.ICountInfoService;
import com.ruoyi.market.enums.ManagerTypeEnum;
import com.ruoyi.special.service.ISpecialCostomManagerInfoService;
import com.ruoyi.web.wapper.CountInfoWapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 渠道统计Controller
 * 
 * @author ruoyi
 * @date 2022-08-11
 */
@Controller
@RequestMapping("/extend/count")
public class CountInfoController extends BaseController
{
    private String prefix = "extend/count";

    @Autowired
    private ICountInfoService countInfoService;
    @Autowired
    private IChannelInfoService channelInfoService;
    @Autowired
    private ISpecialCostomManagerInfoService specialCostomManagerInfoService;

    @RequiresPermissions("extend:count:view")
    @GetMapping()
    public String count(ModelMap mmap)
    {

        ChannelInfoParam channelInfoParam = new ChannelInfoParam();
        List<Map<String, String>> channelInfoList = channelInfoService.selectSimpleMapInfo(channelInfoParam);
        mmap.put("channelInfoList", channelInfoList);

        List<Map<String, Object>> channelTypeList = ChannelTypeEnum.getShowMap();
        mmap.put("channelTypeList", channelTypeList);

        List<Map<String, Object>> managerInfoList = specialCostomManagerInfoService.getManagerInfoList(ManagerTypeEnum.CHANNEL.getValue());
        mmap.put("managerInfoList", managerInfoList);

        return prefix + "/count";
    }

    /**
     * 查询渠道统计列表
     */
    @RequiresPermissions("extend:count:view")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CountInfoParam countInfoParam)
    {

        // 搜索所有用户的信息
        Map<String, Object> allInfoMap = countInfoService.selectAllCountInfoListMap(countInfoParam);
        if (ObjectUtil.isEmpty(allInfoMap)){
            allInfoMap = new HashMap<>();
        }

        startPage();
        List<Map<String, Object>> mapList = countInfoService.selectCountInfoListMap(countInfoParam);
        mapList = new CountInfoWapper(mapList).wrap();

        mapList.add(0, allInfoMap);

        return getDataTable(mapList);
    }

    @RequiresPermissions("extend:count:mine")
    @GetMapping("mine")
    public String mine(ModelMap mmap)
    {
        SysUser sysUser = getSysUser();

        ChannelInfoParam channelInfoParam = new ChannelInfoParam();
        channelInfoParam.setOwnerId(String.valueOf(sysUser.getUserId()));
        List<Map<String, String>> channelInfoList = channelInfoService.selectSimpleMapInfo(channelInfoParam);

        mmap.put("channelInfoList", channelInfoList);

        List<Map<String, Object>> channelTypeList = ChannelTypeEnum.getShowMap();
        mmap.put("channelTypeList", channelTypeList);

        return prefix + "/mine";
    }

    /**
     * 查询渠道统计列表
     */
    @RequiresPermissions("extend:count:mine")
    @PostMapping("/mineList")
    @ResponseBody
    public TableDataInfo mineList(CountInfoParam countInfoParam)
    {

        SysUser sysUser = getSysUser();
        countInfoParam.setOwnerId(String.valueOf(sysUser.getUserId()));

        // 搜索所有用户的信息
        Map<String, Object> allInfoMap = countInfoService.selectAllCountInfoListMap(countInfoParam);
        if (ObjectUtil.isEmpty(allInfoMap)){
            allInfoMap = new HashMap<>();
        }

        startPage();
        List<Map<String, Object>> mapList = countInfoService.selectCountInfoListMap(countInfoParam);
        mapList = new CountInfoWapper(mapList).wrap();

        mapList.add(0, allInfoMap);

        return getDataTable(mapList);
    }

  /**************************** zc临时数据 ***************************************/
  @RequiresPermissions("extend:count:zc")
  @GetMapping("/zc")
  public String zc()
  {
      return prefix + "/zccount";
  }

    /**
     * 查询临时特殊渠道的信息
     * 可查看的渠道信息配置在用户备注信息里面
     */
    @PostMapping("/zclist")
    @ResponseBody
    public TableDataInfo zclist(CountInfoParam countInfoParam)
    {

        SysUser sysUser = getSysUser();
        String remark = sysUser.getRemark();

        String dateInfo = LocalDate.now().toString();
        countInfoParam.setDateInfo(StrUtil.format("{} ~ {}", dateInfo, dateInfo));

        List<Map<String, Object>> mapList = new ArrayList<>();

        if (StrUtil.isNotEmpty(remark)){
            countInfoParam.setChannelCode(remark);
            startPage();
             mapList = countInfoService.selectCountInfoListMap(countInfoParam);
            mapList = new CountInfoWapper(mapList).wrap();
        }

        return getDataTable(mapList);
    }
  /**************************** zc临时数据 ***************************************/

    /**
     * 导出渠道统计列表
     */
    @RequiresPermissions("extend:count:export")
    @Log(title = "渠道统计", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CountInfo countInfo)
    {
        List<CountInfo> list = countInfoService.selectCountInfoList(countInfo);
        ExcelUtil<CountInfo> util = new ExcelUtil<CountInfo>(CountInfo.class);
        return util.exportExcel(list, "渠道统计数据");
    }

    /**
     * 新增渠道统计
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存渠道统计
     */
    @RequiresPermissions("extend:count:add")
    @Log(title = "渠道统计", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CountInfo countInfo)
    {
        return toAjax(countInfoService.insertCountInfo(countInfo));
    }

    /**
     * 修改渠道统计
     */
    @RequiresPermissions("extend:count:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CountInfo countInfo = countInfoService.selectCountInfoById(id);
        mmap.put("countInfo", countInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存渠道统计
     */
    @RequiresPermissions("extend:count:edit")
    @Log(title = "渠道成本修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CountInfo countInfo)
    {
        return toAjax(countInfoService.updateCountInfo(countInfo));
    }

    /**
     * 删除渠道统计
     */
    @RequiresPermissions("extend:count:remove")
    @Log(title = "渠道统计", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(countInfoService.deleteCountInfoByIds(ids));
    }


}
